var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var root = ui("$");
var http = require("helper/http");
var mallUrl = require("url/mall");

var shopId = null;
var sendDeliveryType = null;
var shopStyle = null;
var swtxt1 = [ 0, " ", " " ]; // 0:开关状态 1:关闭状态文字 2:开启状态文字
var swtxt2 = [ 0, " ", " " ]; // 0:开关状态 1:关闭状态文字 2:开启状态文字
var swtxt3 = [ 0, " ", " " ]; // 0:开关状态 1:关闭状态文字 2:开启状态文字
var sendDeliveryType1 = '',sendDeliveryType2 = '',sendDeliveryType3 = '';
var params = page.getData();
shopId = params.id;
sendDeliveryType = params.sendDeliveryType;
shopStyle = params.shopStyle;
var linearLayout_main = ui("do_LinearLayout_main");
var switchtxt3 = linearLayout_main.add("txtswitch3","source://view/reuse/form/mutiSwitch.ui");
var switchtxtS3 = ui("txtswitch3");
var switchtxt2 = linearLayout_main.add("txtswitch2","source://view/reuse/form/mutiSwitch.ui");
var switchtxtS2 = ui("txtswitch2");
if(shopStyle == 1){//邻里店
	switchtxtS3.visible = false;
}else if(shopStyle == 3){//便利店
	switchtxtS2.visible = false;
}else{//社区店铺
	switchtxtS2.visible = true;
	switchtxtS3.visible = true;
}
var temp = sendDeliveryType.split(";");
for (var i=0; i < temp.length; i++) {//初始化
	if (temp[i] == '1') {//买家自提
		swtxt1 = [ 1, " ", " " ];
		sendDeliveryType1 = '1';
	}
	if (temp[i] == '2') {//店铺配送
		swtxt2 = [ 1, " ", " " ];
		sendDeliveryType2 = '2';
	}
	if ((shopStyle == 2 || shopStyle == 3) && temp[i] == '3') {//圈圈配送(社区店铺\便利店)
		swtxt3 = [ 1, " ", " " ];
		sendDeliveryType3 = '3';
	}
}

//返回键
page.on("back", function() {
    app.closePage();
});

var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var header = ui("header");
ui(addheader + ".labelTitle").text = "修改店铺配送类型";
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage();
});

//保存
var headerRight = ui(addheader+".headerRight");
var headerRight_lableNext = ui(addheader+".labelNext");
headerRight_lableNext.text = "保存";
headerRight_lableNext.visible = true;
headerRight.on("touch", function() {
	if (sendDeliveryType1 == '' && sendDeliveryType2 == '' && sendDeliveryType3 == '') {
		nf.toast("至少选择一项");
		return;
	}
	submit();
});

function submit(){
	var sendDeliveryTypeStr = '';
	var arr = [];
	if (sendDeliveryType1 != '') {
		arr.push(sendDeliveryType1);
	}
	if (sendDeliveryType2 != '') {
		arr.push(sendDeliveryType2);
	}
	if(sendDeliveryType3 != ''){
		arr.push(sendDeliveryType3);
	}
	if(arr.length > 1){
		sendDeliveryTypeStr = arr.join(";");	
	}else{
		sendDeliveryTypeStr = arr[0];
	}
	var paramData = {
		id:shopId,
		sendDeliveryType:sendDeliveryTypeStr
	};
	http.post(mallUrl.url.shop.updateSendDeliveryType, paramData, function(data) {
		app.fire("fireShopUpdate",{"sendDeliveryTypeStr":data.mallShop.sendDeliveryTypeStr,"sendDeliveryType":data.mallShop.sendDeliveryType});
		app.closePage();
	});
};


/** -------------------add UI start ---------------------------**/
var switchtxt1 = linearLayout_main.add("txtswitch1","source://view/reuse/form/mutiSwitch.ui");
var switchtxtS1 = ui("txtswitch1");
var labelTitle1 = ui(switchtxt1 + ".labelTitle");
var aLayoutRoot1 = ui(switchtxt1 + ".aLayoutRoot");
aLayoutRoot1.tag=1;
switchtxtS1.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "买家自提"
})
switchtxtS1.refreshData();

switchtxtS1.on("switchtag", function(data) {
	if (data == 0) {// 关闭，不允许
		sendDeliveryType1 = '';
	}
	if (data == 1) {// 打开，允许
		sendDeliveryType1 = '1';
	}
});
switchtxtS1.fire("switchzt", swtxt1);

var labelTitle2 = ui(switchtxt2 + ".labelTitle");
var aLayoutRoot2 = ui(switchtxt2 + ".aLayoutRoot");
aLayoutRoot2.tag=1;
switchtxtS2.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "店铺配送"
})
switchtxtS2.refreshData();

switchtxtS2.on("switchtag", function(data) {
	if (data == 0) {// 关闭，不允许
		sendDeliveryType2 = '';
	}
	if (data == 1) {// 打开，允许
		sendDeliveryType2 = '2';
	}
});
switchtxtS2.fire("switchzt", swtxt2);

var labelTitle3 = ui(switchtxt3 + ".labelTitle");
var aLayoutRoot3 = ui(switchtxt3 + ".aLayoutRoot");
aLayoutRoot3.tag=1;
switchtxtS3.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "圈圈配送"
})
switchtxtS3.refreshData();

switchtxtS3.on("switchtag", function(data) {
	if (data == 0) {// 关闭，不允许
		sendDeliveryType3 = '';
	}
	if (data == 1) {// 打开，允许
		sendDeliveryType3 = '3';
	}
});
switchtxtS3.fire("switchzt", swtxt3);
/** -------------------add UI end ---------------------------**/
